$(document).ready(function () {
    $("#add").click(function () {
        var name = $("#tname").val();
        var money = $("#tm").val();
        var startTime = $("#tt1").val();
        var stopTime = $("#tt2").val();
        var checkLocation = $("#checkLocation").val();
        var location = "";
        $('input[name="header"]:checked').each(function(i){
            if(0==i){
                location = $(this).val();
            }else{
                location+= (","+$(this).val());
            }
        });
        if(name==null||money==null||name==""||money==""){
            alert("请完善所有信息!!!!");
        }
        else {
            // alert("checkLocation  ==  "+checkLocation);
            $.ajax({
                type: "POST",
                url: "/daily/insertTo",
                dataType: "text",
                data: {
                    name: name,
                    money: money,
                    startTime: startTime,
                    stopTime: stopTime,
                    checkLocation:checkLocation,
                    location:location,
                },
                success: function (data) {
                    console.log(data);
                 if(data==1){
                     alert("添加成功!!")
                 }
                 else {
                     alert("失败!!!")
                 }

                    $("#tname").val("");   $("#tm").val("");
                 $("#tt1").val("");
                    $("#tt2").val("");
                    $("#t1 tr:not(:first)").empty();
                      getDaily();
                    $('input:checkbox').removeAttr('checked');
                },
                error: function (xhr, textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });}

    });

        getDaily();
    function getDaily() {
        $.getJSON("/daily/getDaily", function (json) {
            $.each(json, function (i, item) {
                console.log("item.id"+item.id);
                $("#t1").append("<tr><td>" + item.id + "</td><td>" + item.name + "</td><td>" +
                    item.money+"</td><td>"+item.startTime+"</td><td>"+
                    item.stopTime +"</td><td>" +item.checkLocation+"</td><td>"+item.location+"</td><td>"+ "<input type='button' class='del' value='删除' /> "+ "</td></tr>");

            });
            $(".del").click(function () {
          var id = $(this).parent().parent().find("td").eq(0).text();
          alert(id);
                $.ajax({
                    type: "POST",
                    url: "/daily/deleteDaily",
                    dataType: "json",
                    data: {
                        id: id,
                    },
                    success: function (data) {
                        console.log(data);
                        if(data==1){
                            alert("删除成功!!")
                        }
                        else {
                            alert("失败!!!")
                        }

                    },
                    error: function (xhr, textStatus) {
                        console.log('错误');
                        console.log(xhr);
                        console.log(textStatus);
                    }
                });
                $(this).parent().parent().remove();

            });

        });
    }

    $.ajax({
        type: "GET",
        url: "/location/getLocations",
        dataType: "json",
        data: {
        },
        success: function (data) {
            console.log(data);
            for (var i in data) {
                $("#location").append("<input type='checkbox' value='"+data[i].id+"' name='header'/>"+data[i].name);
            }
        },
        error: function (xhr, textStatus) {
            console.log('错误');
            console.log(xhr);
            console.log(textStatus);
        }
    });

    checkLocation();
$("#checkLocation").change(function () {
     checkLocation();
})

    function checkLocation() {
        if($("#checkLocation").val()==0){
            $("#r1").hide();
        }else{
            $("#r1").show();
        }
    }
});




